﻿using ETWJ.DTO;
using ETWJ.Iservice;
using ETWJ.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ETWJ.Service
{
    public class PermissisonService : CommonService<Permission, PermissionDTO, MyDbContext>, IPermissionService<Permission, PermissionDTO>
    {
        public async Task<int> Update(PermissionDTO dto)
        {
            using (MyDbContext ctx = new MyDbContext())
            {
                BaseService<Permission> bs = new BaseService<Permission>(ctx);

                var data = await bs.GetByIdAsync(dto.Id);
                data.Name = dto.Name;
                data.Description = dto.Description;
                return await ctx.SaveChangesAsync();
            }
        }

        public async Task<List<RolePermissionDTO>> GetPermissionByRoleIDAsync(int roleId)
        {
            using (MyDbContext ctx = new MyDbContext())
            {
                List<RolePermissionDTO> list = new List<RolePermissionDTO>();
                BaseService<Permission> bs = new BaseService<Permission>(ctx);
                await ctx.Set<RolePermission>().Where(e => e.RoleId == roleId&&e.IsDeleted==0).Include(e=>e.Role).Include(e => e.Permission).AsNoTracking().ForEachAsync(e =>
                {
                    list.Add(AutoMapper.Mapper.Map<RolePermissionDTO>(e));
                });
                return list;
                //ctx.Set<Permission>
            }
        }

        public async Task<int> GetRoleDistrubutionPermAsync(int roleId,int[] permissionId)
        {
            using (MyDbContext ctx = new MyDbContext())
            {

                BaseService<RolePermission> bs = new BaseService<RolePermission>(ctx);
                RolePermission[] role = await bs.GetAll().Where(e => e.RoleId == roleId).ToArrayAsync();
                if(role.Count()>0) // 说明该角色存在权限，则删除他的权限，然后重新给他添加权限
                {
                    var rolePermissions =await GetPermissionByRoleIDAsync(roleId);
                    foreach (var item in rolePermissions)
                    {
                        await bs.RealDeleted(item.Id);
                    }
                }

                for (int i = 0; i < permissionId.Length; i++)
                {
                    RolePermission rp = new RolePermission();
                    rp.RoleId = roleId;
                    rp.PermissionId = permissionId[i];
                    await ctx.Set<RolePermission>().AddAsync(rp);
                }
                return await ctx.SaveChangesAsync();
            }
        }

        public async Task<PermissionDTO> GetPermissionByName(string permissionName)
        {
            using (MyDbContext ctx = new MyDbContext())
            {
                var data = await ctx.Set<Permission>().Where(e => e.Name == permissionName&&e.IsDeleted==0).AsNoTracking().SingleOrDefaultAsync();
                return AutoMapper.Mapper.Map<PermissionDTO>(data);
            }
        }
    }
}
